package com.netease.web;

import com.netease.domain.*;
import com.netease.service.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

/**
 * Created by dongle on 2017/2/21.
 */
@Controller
public class HomeController implements ApplicationContextAware{
    @Autowired
    private LoadBalanceService loadBalanceService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private IPService ipService;
    @Autowired
    private CloudDiskService cloudDiskService;
    @Autowired
    private NamespaceService namespaceService;
    @Autowired
    private MicroserviceService microserviceService;

    @Autowired
    private AccessTokenService accessTokenService;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/loadbalance")
    public String loadbalance(Model model) throws IOException {
        List<LoadBalanceItem> list = loadBalanceService.getList();
        model.addAttribute("list", list);
        return "loadbalance";
    }

    @RequestMapping("/repository")
    public String repository(Model model) throws IOException {
        List<RepositoryItem> list = repositoryService.getList();
        model.addAttribute("list", list);
        return "repository";
    }

    @RequestMapping("/ips")
    public String ips(Model model) throws IOException {
        List<IPItem> list = ipService.getList();
        model.addAttribute("list", list);
        return "ips";
    }

    @RequestMapping("/clouddisk")
    public String clouddisk(Model model) throws IOException {
        List<CloudDiskItem> list = cloudDiskService.getList();
        model.addAttribute("list", list);
        return "clouddisk";
    }

    @RequestMapping("microservice")
    public String microservice(Model model) throws IOException {
        List<NamespaceItem> spaceList = namespaceService.getList();
        for (NamespaceItem spaceItem:
             spaceList) {
            List<MicroserviceItem> serviceList = microserviceService.getListsInNamesapce(spaceItem.getId());
            spaceItem.setServiceList(serviceList);
        }
        model.addAttribute("spaceList", spaceList);
        return "microservice";
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        accessTokenService.startTokenService();
    }
}
